var connection = require('./connection');
var time = require('./time');

function insertCategory(id, title, cover_img) {
    let sql = "INSERT INTO category(id, title, cover_img, modify_time) VALUES(?, ?, ?, ?)";
    let sqlParams = [id, title, cover_img, time()];
    return new Promise(function(resolve, reject) {
        connection.query(sql, sqlParams, function(err, res) {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        })
    });
}

/**
 * 编辑类别
 * @param {*} id
 * @param {*} title
 * @param {*} cover_img
 */
function editCate(id, title, cover_img) {
    let sql = "update category set title=?, cover_img=?, modify_time=? where id='" + id + "'";
    let sqlParams = [title, cover_img, time()];
    return new Promise(function(resolve, reject) {
        connection.query(sql, sqlParams, function(err, res) {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        })
    });
}


/**
 * 删除类别
 * @param {*} id
 */
function deleteCate(id) {
    let sql = "delete from category where id='" + id + "'";
    return new Promise(function(resolve, reject) {
        connection.query(sql, function(err, res) {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        })
    });
}

function insertArticle(id, title, cover_img, content, category_id) {
    let sql = "INSERT INTO article(id, title, cover_img, content, category_id, modify_time) VALUES(?, ?, ?, ?, ?, ?)";
    let sqlParams = [id, title, cover_img, content, category_id, time()];
    return new Promise(function(resolve, reject) {
        connection.query(sql, sqlParams, function(err, res) {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        })
    });
}

/**
 * 修改文章
 * @param {*} id
 * @param {*} title
 * @param {*} cover_img
 * @param {*} content
 * @param {*} category_id
 */
function editArticle(id, title, cover_img, content, category_id) {
    let sql = "update article set title=?, cover_img=?, content=?, category_id=?, modify_time=? where id='" + id + "'";
    let sqlParams = [title, cover_img, content, category_id, time()];
    return new Promise(function(resolve, reject) {
        connection.query(sql, sqlParams, function(err, res) {
            if (err) {
                console.log(err);
                reject(err);
            } else {
                resolve(res);
            }
        })
    });
}

/**
 * 删除文章
 */
function deleteArticle(id) {
    let sql = "delete from article where id='" + id + "'";
    return new Promise(function(resolve, reject) {
        connection.query(sql, function(err, res) {
            if (err) {
                console.log(err);
                reject(err);
            } else {
                resolve(res);
            }
        })
    });
}

/**
 * 添加推荐
 */
function addHot(id) {
    let sql = "insert into hot(id) values(?)";
    let sqlParams = [id];
    return new Promise(function(resolve, reject) {
        connection.query(sql, sqlParams, function(err, res) {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        })
    });
}

/**
 * 取消推荐
 */
function removeHot(id) {
    let sql = "delete from hot where id='" + id + "'";
    return new Promise(function(resolve, reject) {
        connection.query(sql, function(err, res) {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        })
    });
}

module.exports = {
    insertCategory: insertCategory,
    editCate: editCate,
    deleteCate: deleteCate,
    insertArticle: insertArticle,
    editArticle: editArticle,
    deleteArticle: deleteArticle,
    addHot: addHot,
    removeHot: removeHot
}
